package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.shiguo365.market.R;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IRegisterView;
import com.shiguo365.market.presenter.implement.RegisterPresenter;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements IRegisterView {

    RegisterPresenter registerPresenter;

    private boolean result = true;
    private int time = 60;
    private int count = 0;

    @BindView(R.id.editTextRegUserName) EditText mEdtRegUserName;
    @BindView(R.id.editTextRegSMSCode) EditText mEdtRegSMSCode;
    @BindView(R.id.editTextRegUserPassword) EditText mEdtUserPassword;
    @BindView(R.id.editTextRegUserPasswordAgain) EditText mEdtRegUserPasswordAgain;
    @BindView(R.id.editTextRegShopName) EditText mEdtRegShopName;
    @BindView(R.id.editTextRegShopPerson) EditText mEdtRegShopPerson;
    @BindView(R.id.spinnerRegShopProvince) Spinner mEdtRegShopProvince;
    @BindView(R.id.spinnerRegShopCity) Spinner mEdtRegShopCity;
    @BindView(R.id.editTextRegShopAddress) EditText mEdtRegShopAddress;
    @BindView(R.id.buttonSendSMSCode ) Button mBtnSendSMSCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        this.setTitle("用户注册");
        ButterKnife.bind(this); //ButterKnife初始化
        registerPresenter = new RegisterPresenter(this,this);
    }

    @OnClick(R.id.buttonReg)
    public void register(){
        String username=mEdtRegUserName.getText().toString();
        String code=mEdtRegSMSCode.getText().toString();
        String pwd=mEdtUserPassword.getText().toString();
        String pwd_cfg=mEdtRegUserPasswordAgain.getText().toString();
        String shopName=mEdtRegShopName.getText().toString();
        String realName=mEdtRegShopPerson.getText().toString();
        String province=mEdtRegShopProvince.getSelectedItem().toString();
        String city=mEdtRegShopCity.getSelectedItem().toString();
        String street=mEdtRegShopAddress.getText().toString();

        if("".equals(username)){
            this.showRegisterMessages("手机号不能为空");
            return;
        }
        if("".equals(code)){
            this.showRegisterMessages("验证码不能为空");
            return;
        }
        if("".equals(pwd)){
            this.showRegisterMessages("登录密码不能为空");
            return;
        }
        if("".equals(pwd_cfg)){
            this.showRegisterMessages("确认登录密码不能为空");
            return;
        }
        if(!pwd.equals(pwd_cfg)){
            this.showRegisterMessages("登录密码和确认登录密码不一致");
            return;
        }
        if("".equals(shopName)){
            this.showRegisterMessages("公司名称不能为空");
            return;
        }
        if("".equals(realName)){
            this.showRegisterMessages("您的姓名不能为空");
            return;
        }
        if("".equals(province)){
            this.showRegisterMessages("省份不能为空");
            return;
        }
        if("".equals(city)){
            this.showRegisterMessages("城市不能为空");
            return;
        }
        if("".equals(street)){
            this.showRegisterMessages("公司地址不能为空");
            return;
        }
        registerPresenter.register(username,code,pwd,shopName,realName,province,city,street);
    }

    @OnClick(R.id.buttonSendSMSCode)
    public void smscheck(){
        String mobile=mEdtRegUserName.getText().toString();

        if("".equals(mobile)){
            this.showRegisterMessages("手机号不能为空");
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (result) {
                    time--;
                    try {
                        Thread.sleep(1000);
                        mBtnSendSMSCode.post(new Runnable() {
                            @Override
                            public void run() {
                                mBtnSendSMSCode.setText(time + "s后重新获取");
                                mBtnSendSMSCode.setClickable(false);
                            }
                        });
                        if (time <= 1) {
                            count=0;
                            result = false;
                            mBtnSendSMSCode.post(new Runnable() {
                                @Override
                                public void run() {
                                    mBtnSendSMSCode.setText("获取验证码");
                                    mBtnSendSMSCode.setClickable(true);
                                }
                            });
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                result = true;
                time = 60;
            }
        }).start();

        registerPresenter.smscheck(mobile);
    }

    //显示消息
    @Override
    public void showRegisterMessages(String msg) {
        ToastUtils.with(this).show(msg);
    }

    //注册成功后进入登录页面
    @Override
    public void registerSuccess() {
        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

}



